package club.kingyin.kyblogprofile.controller;

import club.kingyin.commons.constant.AuthorizeConstant;
import club.kingyin.commons.entity.file.Words;
import club.kingyin.commons.enums.BlogEnum;
import club.kingyin.commons.response.Result;
import club.kingyin.easycache.annotation.EasyCache;
import club.kingyin.kyblogprofile.core.es.IkService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/file/ik")
public class IkController {

    @Resource
    private IkService ikService;

    @GetMapping("/keyword")
    public Result getRandomWords(@RequestParam("size") int size) {
        return Result.success(ikService.random(size));
    }

    @EasyCache(module = "ky-file",prams ="name")
    @GetMapping("/words")
    public Result ikWords(@RequestHeader(AuthorizeConstant.ROLE) String role,
                          @RequestParam("name") String name) throws UnsupportedEncodingException {
        switch (name) {
            case IkService.WORD:
                return Result.success(filterWords(ikService.getWords()));
            case IkService.STOP:
                if (!BlogEnum.ROOT.getDesc().equals(URLDecoder.decode(role,"UTF8"))) {
                    return Result.failR("权限不足");
                }
                return Result.success(ikService.getStops());
            default:return Result.failN("操作失败");
        }

    }

    private final List<String> cla = Arrays.asList("学习中心", "随心记录", "科学技术", "体育休闲", "游戏", "新青年", "设计创作", "时尚艺术", "生活娱乐", "其他");

    private Set<String> filterWords(Set<String> set) {
        set.removeIf(cla::contains);
        return set;
    }

    @EasyCache(prams = "words.name",delete = "ikWords", module = "ky-file")
    @PostMapping("/words/add")
    public Result ikAddWords(@RequestHeader(AuthorizeConstant.ROLE) String role,
                             @RequestBody Words words) throws UnsupportedEncodingException {
        if (!BlogEnum.ROOT.getDesc().equals(URLDecoder.decode(role,"UTF8"))) {
            return Result.failR("权限不足");
        }
        int res;
        switch (words.getName()) {
            case IkService.WORD:
                res = ikService.addWord(words.getWords());
                break;
            case IkService.STOP:
                res = ikService.addStop(words.getWords());
                break;
            default:
                return Result.failN("操作失败");
        }
        return res>0?Result.infoN("添加成功"):res==0?Result.infoN("已经存在了"):Result.failR("添加失败");
    }

    @EasyCache(prams = "words.name",delete = "ikWords", module = "ky-file")
    @PostMapping("/words/remove")
    public Result ikRemoveWords(@RequestHeader(AuthorizeConstant.ROLE) String role,
                                @RequestBody Words words) throws UnsupportedEncodingException {
        if (!BlogEnum.ROOT.getDesc().equals(URLDecoder.decode(role,"UTF8"))) {
            return Result.failR("权限不足");
        }
        int res;
        switch (words.getName()) {
            case IkService.WORD:
                res = ikService.removeWord(words.getWords());
                break;
            case IkService.STOP:
                res = ikService.removeStop(words.getWords());
                break;
            default:
                return Result.failN("操作失败");
        }
        return res>0?Result.infoN("删除成功"):res==0?Result.infoN("没有找到"):Result.failR("删除失败");
    }

}
